package com.extra_archive.project.iisu.view
{
	import iisu.util.GestureModel;

	import com.extra_archive.framework.core.mvc.controller.IController;
	import com.extra_archive.framework.core.mvc.model.IModel;
	import com.extra_archive.framework.core.mvc.view.CompositeView;
	import com.extra_archive.project.iisu.model.BasicModelEvent;
	import com.extra_archive.project.iisu.model.IBasicModel;

	import org.papervision3d.view.stats.StatsView;

	import flash.events.Event;

	/**
	 * @author vkimone
	 * 
	 */
	public class BasicMediatorView extends CompositeView 
	{
		private var _gesture : GestureModel; 
		
		protected var _debugView : BasicDebugView;
		protected var _space3dView : BasicSpace3DView;

		private var _pv3dStatsView : StatsView;

		public function BasicMediatorView(model : IModel = null, controller : IController = null)
		{
			super(model, controller);
			
			_gesture = IBasicModel(_model).gesture;
			
			_model.addEventListener(BasicModelEvent.START_APPLICATION, onStartApplication);
		}

		//--------------------------------------------------------------------------
		//
		//    Public Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * create space3d view and debug view
		 * add EventerFrameEvent for update views
		 */
		override public function initialize() : void 
		{
			trace(this + " :  initialize");
					
			createSpace3DView();
			createDebugView();
			createPVStatsView();
			
			addEventListener(Event.ENTER_FRAME, update);
			
			//TODO : create other views
		}

		/**
		 * update views
		 */
		override public function update(event : Event = null) : void 
		{
			super.update();
			
			_debugView.visible = _pv3dStatsView.visible = IBasicModel(_model).isDebugViewActive;
		}

		override public function toString() : String 
		{
			return "com.dstrict.project.softkinetic.view.BasicMediatorView";
		}
		
		//--------------------------------------------------------------------------
		//
		//    Event Listeners
		//
		//--------------------------------------------------------------------------
		
		protected function onStartApplication(event : BasicModelEvent) : void 
		{
			initialize();
		}

		//--------------------------------------------------------------------------
		//
		//    Private Methods
		//
		//--------------------------------------------------------------------------

		protected function createSpace3DView() : void 
		{
			_space3dView = new BasicSpace3DView();
			
			addChild(_space3dView);
			addComponent(_space3dView);
			
			IBasicModel(_model).space3DView = _space3dView;
		}

		protected function createDebugView() : void 
		{
			_debugView = new BasicDebugView(_model);
			_debugView.y = 75;
			addChild(_debugView);
			addComponent(_debugView);
		}
		
		private function createPVStatsView() : void 
		{
			_pv3dStatsView = new StatsView(_space3dView.renderer);
			addChild(_pv3dStatsView);
		}
	}
}
